package com.mall.shopping.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.ITestProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.TestProductInfoConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.TestItemDto;
import com.mall.shopping.dto.TestProductInfoRequest;
import com.mall.shopping.dto.TestProductInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Classname ITestProductServiceImpl
 * @Description
 * @Date 2021/8/24 12:47
 * @Author terrance_swn
 */
@Slf4j
@Service
public class ITestProductServiceImpl implements ITestProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    TestProductInfoConverter productInfoConverter;

    @Override
    public TestProductInfoResponse getProductInfo(TestProductInfoRequest request) {

        TestProductInfoResponse response = new TestProductInfoResponse();

        try {
            //参数校验
        request.requestCheck();

        //业务逻辑
        Item item = itemMapper.selectByPrimaryKey(request.getProductId());
        TestItemDto itemDto = productInfoConverter.item2ItemDto(item);

        //业务成功返回
        response.setTestItemDto(itemDto);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
    }catch (ValidateException e){
        //可以输出日志log
        response.setCode(e.getErrorCode());
        response.setMsg(e.getMessage());
    } catch (Exception e){
        e.printStackTrace();
        //返回错误信息 这里可以认为是数据库操作出现问题
        response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
        response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
    }

        return response;
    }
}
